package com.wallet.my.util;

import com.wallet.my.contract.Web3jContract.PaiMai;
import com.wallet.my.dao.mapper.ContractMapper;
import com.wallet.my.dao.pojo.ContractDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.utils.Convert;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 合约的工具类
 **/
@Component
public class ContractUtil {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private EthUtil ethUtil;

//    返回load一个PaiMai合约的对象，固定privateKey生成的，这样新用户进来就可以使用查询功能
    public PaiMai loadPaiMaiContract(String contractAddress, Web3j web3j){
        String privateKey="8dae8f85c9a1a1d385bd4f1fa38d79134f00ef3445c794c858cb30a36531ebda";
        Credentials credentials = Credentials.create(privateKey);
        BigInteger gasPrice = ethUtil.getMyGasPrice(web3j);
        BigInteger gasLimit = new BigInteger("500000");
        PaiMai load = PaiMai.load(contractAddress, web3j, credentials, gasPrice, gasLimit);
        return load;
    }

//    更新所有拍卖合约的状态和最高出价信息
    public void updataPaiMaiContracts(Web3j web3j){
        List<String> allPaiMaiContractAddress = contractMapper.getAllPaiMaiContractAddress();
        ContractDO contractDO = new ContractDO();
        String privateKey="8dae8f85c9a1a1d385bd4f1fa38d79134f00ef3445c794c858cb30a36531ebda";
        String status1="ended";
        Credentials credentials = Credentials.create(privateKey);
        BigInteger gasPrice = ethUtil.getMyGasPrice(web3j);
        BigInteger gasLimit = new BigInteger("500000");
        for (String contractAddress:allPaiMaiContractAddress){
//            这里判断状态是已结束，跳出循环，增加代码效率
            String contractStatus = contractMapper.paiMaiContractStatus(contractAddress);
            if (status1.equals(contractStatus)){
                continue;
            }else {
                PaiMai load = PaiMai.load(contractAddress, web3j, credentials, gasPrice, gasLimit);
                String highestBidder = null;
                String highestBid = null;
                BigInteger auctionEndTime=null;
                try {
                    auctionEndTime = load.auctionEndTime().send();
                    highestBidder = load.highestBidder().send();
                    highestBid = load.highestBid().send().toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (auctionEndTime!=null){

                    long nowTime = new Date().getTime();
                    long endTimeLong = auctionEndTime.longValue()*1000;
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String endTime = sdf.format(new Date(endTimeLong));

                    if (nowTime<endTimeLong){
                        String highestBidValue = Convert.fromWei(highestBid.toString(), Convert.Unit.ETHER).toString();
                        contractDO.setHighestBid(highestBidValue);
                        contractDO.setHighestBidder(highestBidder);
                        contractDO.setContractAddress(contractAddress);
                        contractDO.setEndTime(endTime);
                        contractMapper.updataPaiMaiContracts(contractDO);
                    }else {
                        contractDO.setContractStatus("ended");
                        contractDO.setContractAddress(contractAddress);
                        contractDO.setEndTime(endTime);
                        contractMapper.updataPaiMaiContractStatus(contractDO);
                    }
                }else {
                    try {
                        throw new Exception("updata error");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
}
